// pages/enterprise/home/home.js
var app = getApp();
import {
    ajaxPost,
    showToast
} from '../../../utils/util';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        page: 0,
        limit: 10,
        selectVal: 'online',
        noGet: false,
        listSource: []
    },
    clickSelect(e) {
        if (e.currentTarget.dataset.type !== this.data.selectVal) {
            this.setData({
                page: 0,
                selectVal: e.currentTarget.dataset.type,
            })
            this.getList(true)
        }
    },
    getList(type) {
        let _this = this;
        if (_this.data.noGet) {
            return;
        }
        let data = {
            token: wx.getStorageSync('token'),
            page: ++_this.data.page,
            limit: _this.data.limit,
            source: 'wx',
            type: _this.data.selectVal,
            is_follow: 1,
        };
        ajaxPost("/meeting/meetinglist", data, function (res) {
            if (res.code == 0) {
                let data = res.data;
                if (type) {
                    console.log(type);
                    _this.setData({
                        noGet: false,
                        listSource: data,
                    })
                    return;
                }
                if (data.length == 0) {
                    _this.setData({
                        noGet: true
                    })
                } else {
                    let newarr = _this.data.listSource.concat(data)
                    _this.setData({
                        listSource: newarr,
                    })
                }
            } else {
                wx.showToast({
                    title: res.msg,
                    icon: "none",
                    duration: 2000
                });
            }
        });
    },
    goCont(e) {
        let index = e.currentTarget.dataset.index
        let data = this.data.listSource[index]
        console.log(data.status == 1);
        if (data.status == 1) {
            wx.navigateTo({
                url: "/pages/student/hallRoom/hallRoom?id=" + data.id + '&roomid=' + data.roomid
            });
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.setData({
            page: 0,
        })
        this.getList(true)
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        this.getList()
    },
    /**
     * 关注按钮
     */
    clickFollowed(e) {
        let index = e.currentTarget.dataset.index
        let data = this.data.listSource[index]
        console.log(data);
        if (data.is_follow == 0) {
            let ajaxData = {
                token: wx.getStorageSync('token'),
                meeting_id: data.id,
            }
            ajaxPost('/meeting/followmeeting', ajaxData, (res) => {
                console.log(res);
                if (res.code == 0) {
                    const {
                        listSource
                    } = this.data
                    let newListSource = listSource
                    newListSource[index].student_count = newListSource[index].student_count + 1
                    newListSource[index].is_follow = 1
                    this.setData({
                        listSource: newListSource
                    })
                    showToast('success', res.msg);
                } else {
                    showToast('text', res.msg);
                }
            })

        } else {
            let ajaxData = {
                token: wx.getStorageSync('token'),
                meeting_id: data.id,
            }
            ajaxPost('/meeting/cancelfollowmeeting', ajaxData, (res) => {
                if (res.code == 0) {
                    const {
                        listSource
                    } = this.data
                    let newListSource = listSource
                    newListSource[index].student_count = newListSource[index].student_count - 1
                    newListSource[index].is_follow = 0
                    this.setData({
                        listSource: newListSource
                    })
                    showToast('success', res.msg);
                } else {
                    showToast('text', res.msg);
                }
            })

        }

    },
})